﻿using Framework.Code.Const;
using Framework.Models;
using Framework.Models.Entities;
using Framework.Repository.Data;
using Framework.Utility.Config;
using Framework.Utility.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Framework.Code.Manager;

/// <summary>
/// 用户管理
/// </summary>
public class UserManager : IUserManager
{
    private readonly IRepository<Biz_User,string> _sysUserRep; // 用户表仓储   
    private readonly IHttpContextAccessor _httpContextAccessor;
    public UserManager(IRepository<Biz_User, string> sysUserRep, IHttpContextAccessor httpContextAccessor)
    {
        _sysUserRep = sysUserRep;
        _httpContextAccessor = httpContextAccessor;
    }
    public string UserId
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
    }

    public string Perms
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_PERMS)?.Value;
    }

    public string Account
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_ACCOUNT)?.Value;
    }

    public string Name
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_NAME)?.Value;
    }

    public bool SuperAdmin
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value == AdminType.SuperAdmin;
    }

    public string DeptId
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_DEPTID)?.Value;
    }

    public string Type
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_TYPE)?.Value;
    }

    public string Label
    {
        get => _httpContextAccessor.HttpContext.User.FindFirst(ClaimConst.CLAINM_LABEL)?.Value;
    }

    public Biz_User User
    {
        get => _sysUserRep.GetFirstOrDefault(u => u.Id == UserId);
    }

    /// <summary>
    /// 获取用户信息
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public async Task<Biz_User> GetUserInfo(string userId)
    {
        return await _sysUserRep.GetByKeyAsync(userId); 
    }
}